<?php
/**
 * 创建数据库
 * @author Yangdong Zhang <zhangyangdong@gouuse.cn>
 * @time 2018/10/17 14:24
 */
namespace App\Util;

class Database
{
    /**
     * 创建表 yd_note
     */
    public function note()
    {
        $sql = 'DROP TABLE IF EXISTS yd_note;';
        \think\Db::query($sql);
        //创建表
        $sql = '
CREATE TABLE yd_note (
    id INTEGER  PRIMARY KEY autoincrement not null,
    user_id int not null,
    book_id int not null,
    content text,
    created_at int not null,
    updated_at int not null
)';
        $r = \think\Db::query($sql);
        //创建索引
        $r = \think\Db::query("CREATE INDEX note_user_id ON yd_note (user_id);");
        $r = \think\Db::query("CREATE INDEX note_book_id ON yd_note (book_id);");
    }

    /**
     * 创建表 yd_note_book
     */
    public function noteBook()
    {
        $sql = 'DROP TABLE IF EXISTS yd_note_book;';
        \think\Db::query($sql);
        $sql = '
CREATE TABLE yd_note_book (
    id INTEGER PRIMARY KEY autoincrement not null,
    parent_id int not null default 0,
    user_id int not null,
    title varchar(20) not null,
    type int not null default 0,
    created_at int not null,
    updated_at int not null
)';
        $r = \think\Db::query($sql);
        //创建索引
        $r = \think\Db::query("CREATE INDEX note_book_user_id ON yd_note_book (user_id);");
    }

    /**
     * 创建表 yd_user
     */
    public function user()
    {
        $sql = 'DROP TABLE IF EXISTS yd_user;';
        \think\Db::query($sql);
        $sql = '
   CREATE TABLE yd_user(
   id INTEGER  PRIMARY KEY autoincrement not null,
   username VARCHAR(20) not null UNIQUE,
   password VARCHAR(50) not null,
   nickname varchar(20) not null default \'\',
   email VARCHAR(100) not null,
   created_at int not null,
   updated_at int not null
);';
        $d = \think\Db::query($sql);

        //插入数据
        $data = [
            'username' => 'yangdong',
            'password' => md5('123456'),
            'email'    => '123@qq.com',
            'nickname' => '央东',
            'created_at' => time(),
            'updated_at' => time(),
        ];

        $r = $this->insert($data);
    }
}